# -*- mode: python; coding: utf-8 -*-
#
#
# Copyright © 2006, 2007, 2008 Fluendo Embedded S.L.
# Modified by Francisco Jose Moreno Llorca.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
#
# Author: Loïc Molinari <loic@fluendo.com>
# Modified by Francisco Jose Moreno Llorca <packo@assamita.net>

import sys, pgm, gobject, gst

import pdb

fps_label = None
change = False
newmessage = False

# Key press handler
def on_key_press(viewport, event, pipeline):
    # Terminate the mainloop on 'Escape' or 'q' key press event
    if event.keyval == pgm.keysyms.q or event.keyval == pgm.keysyms.Escape:
        pgm.main_quit()
    # Switch fullscreen state
    if event.keyval == pgm.keysyms.f:
        viewport.fullscreen = not viewport.fullscreen
    # Toggle fps display
    elif event.keyval == pgm.keysyms.p:
        global fps_label
        canvas = viewport.get_canvas()
        if fps_label == None:
            add_fps_display(canvas, viewport)
        else:
            canvas.remove(fps_label)
            fps_label = None

# Drag motion handler
def on_drag_motion(viewport, event, pipeline,laterpipeline):
    # Accept the drag if the extension of the first uri is a media file
    if event.uri[0][-3:] in ("avi", "mpg", "ogg", "mkv", "mov", "wmv", "flv","mp4"):
        return True

# Drag drop handler
def on_drag_drop(viewport, event, pipeline,laterpipeline):
    global change
    if change == False:
        pipeline.set_state(gst.STATE_READY)
        pipeline.set_property('uri', event.uri[0])
        pipeline.set_state(gst.STATE_PLAYING)
        change = True
    else:
        laterpipeline.set_state(gst.STATE_READY)
        laterpipeline.set_property('uri', event.uri[0])
        laterpipeline.set_state(gst.STATE_PLAYING)
        change = False

# Terminate the mainloop on a delete event
def on_delete(viewport, event, pipeline):
    pipeline.set_state(gst.STATE_NULL)
    pgm.main_quit()

def add_fps_display(canvas, viewport):
    global fps_label
    fps_label = pgm.Text()
    fps_label.bg_a = 0
    fps_label.width = canvas.width/10.0
    fps_label.height = canvas.height/20.0
    fps_label.x = canvas.width - fps_label.width
    fps_label.y = canvas.height - fps_label.height
    fps_label.visible = True
    canvas.add(pgm.DRAWABLE_NEAR, fps_label)
    refresh_fps_display(viewport)
    gobject.timeout_add(1000, refresh_fps_display, viewport)

def refresh_fps_display(viewport):
    global fps_label
    if fps_label != None:
        fps_label.label = "%d fps" % viewport.frame_rate
        return True
    else:
        return False
        
def moveMessage(label):
    label.x = (label.x+0.01) %1
    return True

def watchMessages(label):
    try:
        file = open("entrada.txt",'r')
        label.label = file.readlines()[0]
        file.close()
        
    except:
        pass
    return True
    
def main(args):
    # OpenGL viewport creation
    gl = pgm.viewport_factory_make('opengl')
    gl.title = "Sistema multimedia"
    gl.alpha_blending = False
    screen_ratio = float(gl.screen_size_mm[1])/gl.screen_size_mm[0]
    gl.size = (800, 800*screen_ratio)

    # Canvas and image drawable creation
    cvs = pgm.Canvas()
    cvs.size = (1.0, 1.0*screen_ratio)
    
    
    # text
    msg_label = pgm.Text()
    msg_label.label = "esto es una prueba"
    msg_label.bg_a = 0
    msg_label.width = cvs.width/1.0
    msg_label.height = cvs.height/20.0
    msg_label.x = cvs.width - msg_label.width
    msg_label.y = cvs.height - msg_label.height*2
    msg_label.visible = True
    cvs.add(pgm.DRAWABLE_NEAR, msg_label)
    msg_label.label = "esto es una prueba"
    gobject.timeout_add(50, moveMessage, msg_label)
    gobject.timeout_add(5000, watchMessages, msg_label)
    
    
    img = pgm.Image()

    # Bind the canvas to the OpenGL viewport
    gl.set_canvas(cvs)

    # Image properties
    img.position = (0.0, 0.0, 0.0)
    img.size = (0.5,0.3)
    #pdb.set_trace()
    img.bg_color = (0, 0, 255, 0)

    # A drawable needs to be shown
    img.show()

    # Add it to the middle layer of the canvas
    cvs.add(pgm.DRAWABLE_FAR, img)

    # GStreamer pipeline setup initial
    sink = gst.element_factory_make('pgmimagesink')
    pipeline = gst.element_factory_make('playbin')
    pipeline.set_property('video-sink', sink)
    sink.set_property('image', img)
        
    
    laterimg = pgm.Image()
    laterimg.size = (0.5,0.3)
    laterimg.bg_color = (128, 0, 0, 0)
    laterimg.position = (0.5,0.0,0.0)
    laterimg.show()
    cvs.add(pgm.DRAWABLE_MIDDLE,laterimg)
    # GStreamer pipeline setup for later
    latersink = gst.element_factory_make('pgmimagesink')
    laterpipeline = gst.element_factory_make('playbin')
    laterpipeline.set_property('video-sink', latersink)
    latersink.set_property('image', laterimg)

    # Let's start the mainloop
    gl.connect('delete-event', on_delete, pipeline)
    gl.connect('key-press-event', on_key_press, pipeline)
    gl.connect('drag-motion-event', on_drag_motion, pipeline,laterpipeline)
    gl.connect('drag-drop-event', on_drag_drop, pipeline,laterpipeline)
    gl.show()
    pgm.main()

if __name__ == '__main__':
    sys.exit(main(sys.argv[1:]))
